package cn.lr.org.service.impl;

import cn.lr.basic.service.impl.BaseServiceImpl;
import cn.lr.org.domain.Department;
import cn.lr.org.mapper.DepartmentMapper;
import cn.lr.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 部门服务实现类
 */
@Service
//

public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
   private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department); // 返回自增长主键
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        String dirpath="";
        // 新增顶级部门  /+自己id
        if(department.getParent() == null){
            dirpath = ("/" + department.getId()); // 直接拼接 /+自己的id
        } else {
            // 存在上级部门，根据上级部门id查找到上级部门
            Department parent = departmentMapper.loadById(department.getParent().getId());
            dirpath = (parent.getDirPath() + "/" + department.getId()); // 直接找到上级的dirpath，然后在拼接/+自己的id
        }
        department.setDirPath(dirpath);
        departmentMapper.update(department);
    }

//    @Override
    // 嵌套查询，双重for循环，执行效率低--不推荐使用
//    public List<Department> queryTree() {
          // 先查询所有
//        List<Department> allDept = departmentMapper.loadAll();
          // 声明一级部门
//        List<Department> firstDepts = new ArrayList<>();
//        for (Department temp : allDept) {
//            if(temp.getParent_id() == null){ // 如果temp没有上级部门，则其为一级部门
//                firstDepts.add(temp); // 封装第一层级
//            } else { // 其他层级
//                for (Department parent : allDept) {
//                    if(parent.getId() == temp.getParent_id()){ // 找到上一层级，将自己放到上一层级的children中
//                        parent.getChildren().add(temp);
//                        break;  // 结束本层循环
//                    }
//                }
//            }
//        }
//        return firstDepts;
//    }
    @Override
    public List<Department> queryTree() {
        // 查询所有，放到内存中
        List<Department> allDept = departmentMapper.loadAll();
        HashMap<Long, Department> allsMap = new HashMap<>();
        for (Department department : allDept) {
            // 将数据转换成map<Long,department>，减少循环次数，提高运行效率
            allsMap.put(department.getId(), department);
        }
        List<Department> firstDepts = new ArrayList<>();
        for (Department temp : allDept) {
            if(temp.getParent_id() == null){
                firstDepts.add(temp);
            } else {
                Department parent = allsMap.get(temp.getParent_id());
                if(parent != null){ // 找到上一级，将自己放到上一级的children中
                    parent.getChildren().add(temp);
                }
            }
        }
        return firstDepts;
    }
}
